/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.diagram.activity.helper;

import org.eclipse.uml2.uml.AddStructuralFeatureValueAction;
import org.eclipse.uml2.uml.AddVariableValueAction;
import org.eclipse.uml2.uml.CallOperationAction;
import org.eclipse.uml2.uml.DestroyObjectAction;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.InputPin;
import org.eclipse.uml2.uml.InvocationAction;
import org.eclipse.uml2.uml.LiteralUnlimitedNatural;
import org.eclipse.uml2.uml.OpaqueAction;
import org.eclipse.uml2.uml.SendObjectAction;
import org.eclipse.uml2.uml.SendSignalAction;
import org.eclipse.uml2.uml.UMLFactory;
import org.eclipse.uml2.uml.UnmarshallAction;

/**
 * Manages an InputPin
 *
 * @author Gilles Cannenterre
 * @since 26 mars 07
 */
public class NodeInputPinHelper extends NodePinHelper {

	/* (non-Javadoc)
	 * @see com.cea.papyrus.core.common.helper.NodeHelper#createUMLElement(java.lang.String, org.eclipse.uml2.uml.Element)
	 */
	@Override
	public Element createUMLElement(String name, Element umlParent){
		InputPin aPin = null;
		if (umlParent instanceof SendObjectAction) {
			if  ( ((SendObjectAction)umlParent).getTarget() == null ) {
				aPin = ((SendObjectAction)umlParent).createTarget(name, null); 
			} else if ( ((SendObjectAction)umlParent).getRequest() == null ) {
				aPin = ((SendObjectAction)umlParent).createRequest(name, null); 
			}
		}else if (umlParent instanceof OpaqueAction) {
			aPin = ((OpaqueAction)umlParent).createInputValue(name, null);} 
		else if (umlParent instanceof InvocationAction) {
			aPin = ((InvocationAction)umlParent).createArgument(name, null);
		} else if (umlParent instanceof UnmarshallAction) {
			aPin = ((UnmarshallAction)umlParent).createObject(name, null);
		} else if (umlParent instanceof AddVariableValueAction) {
			aPin = ((AddVariableValueAction)umlParent).createInsertAt(name, null);
		}
		 else if (umlParent instanceof DestroyObjectAction) {
				aPin = ((DestroyObjectAction)umlParent).createTarget(name, null);}
		 else if (umlParent instanceof AddStructuralFeatureValueAction) {
				aPin = ((AddStructuralFeatureValueAction)umlParent).createValue(name, null);}
		if (aPin != null) {
			LiteralUnlimitedNatural thenatural=UMLFactory.eINSTANCE.createLiteralUnlimitedNatural();
			thenatural.setValue(LiteralUnlimitedNatural.UNLIMITED);
			aPin.setUpperBound(thenatural);
		}
		return aPin;
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.core.common.helper.NodeHelper#getBaseString()
	 */
	@Override
	public String getBaseString() {
		return "InputPin_"; //$NON-NLS-1$
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.core.common.helper.NodeHelper#addUMLElement(org.eclipse.uml2.uml.Element, org.eclipse.uml2.uml.Element)
	 */
	@Override
	public boolean addUMLElement(Element element, Element umlParent) {
		if (umlParent instanceof SendObjectAction) {
			if  ( ((SendObjectAction)umlParent).getTarget() == null ) {
				((SendObjectAction)umlParent).setTarget((InputPin) element); 
				return true;
			} else if ( ((SendObjectAction)umlParent).getRequest() == null ) {
				((SendObjectAction)umlParent).setRequest((InputPin) element); 
				return true;
			}
		}
		if (umlParent instanceof SendSignalAction) {
			return ((SendSignalAction)umlParent).getArguments().add((InputPin)element); 
		}
		if (umlParent instanceof CallOperationAction) {
			return ((CallOperationAction)umlParent).getArguments().add((InputPin)element); 
		}
		if (umlParent instanceof InvocationAction) {
			return ((InvocationAction)umlParent).getArguments().add((InputPin)element); 
		}
		if (umlParent instanceof UnmarshallAction) {
			((UnmarshallAction)umlParent).setObject((InputPin) element);
			return true;
		}
		if (umlParent instanceof AddVariableValueAction) {
			((AddVariableValueAction)umlParent).setInsertAt((InputPin) element);
			return true;
		}
		if (umlParent instanceof DestroyObjectAction) {
			((DestroyObjectAction)umlParent).setTarget((InputPin) element);
			return true;
		}
		if (umlParent instanceof AddStructuralFeatureValueAction) {
			((AddStructuralFeatureValueAction)umlParent).setValue((InputPin) element);
			return true;}
		
		return false;
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.core.common.helper.NodeHelper#removeUMLElement(org.eclipse.uml2.uml.Element, org.eclipse.uml2.uml.Element)
	 */
	@Override
	public boolean removeUMLElement(Element element, Element umlParent) {
		if (umlParent instanceof SendObjectAction) {
			if  ( ((SendObjectAction)umlParent).getTarget() != null ) {
				((SendObjectAction)umlParent).setTarget(null); 
				return true;
			} else if ( ((SendObjectAction)umlParent).getRequest() != null ) {
				((SendObjectAction)umlParent).setRequest(null); 
				return true;
			}
		}		
		if (umlParent instanceof SendSignalAction) {
			return ((SendSignalAction)umlParent).getArguments().remove(element); 
		}
		if (umlParent instanceof CallOperationAction) {
			return ((CallOperationAction)umlParent).getArguments().remove(element); 
		}
		if (umlParent instanceof UnmarshallAction) {
			((UnmarshallAction)umlParent).setObject(null);
			return true;
		}
		if (umlParent instanceof AddVariableValueAction) {
			((AddVariableValueAction)umlParent).setInsertAt(null);
			return true;
		}
		if (umlParent instanceof DestroyObjectAction) {
			((DestroyObjectAction)umlParent).setTarget(null);
			return true;
		}
		if (umlParent instanceof AddStructuralFeatureValueAction) {
			((AddStructuralFeatureValueAction)umlParent).setValue(null);
			return true;
		}
		return false;
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.core.common.helper.NodeHelper#canCreateUMLElement(org.eclipse.uml2.uml.Element)
	 */
	@Override
	public boolean canCreateUMLElement(Element umlParent) {	
		if ( (umlParent instanceof SendObjectAction) 
			&& ((((SendObjectAction)umlParent).getTarget() == null) 
			|| (((SendObjectAction)umlParent).getRequest() == null)) ) {
			return true;
		}
		
		if ( (umlParent instanceof InvocationAction)||(umlParent instanceof OpaqueAction) ) {
			return true;
		}
		if ( (umlParent instanceof UnmarshallAction) 
			&& (((UnmarshallAction)umlParent).getObject() == null) ) {
			return true;
		}
		if ( (umlParent instanceof AddVariableValueAction) 
			&& (((AddVariableValueAction)umlParent).getInsertAt() == null) ) {
			return true;
		}
		if ( (umlParent instanceof DestroyObjectAction))  {
				return true;
			}
		if ( (umlParent instanceof AddStructuralFeatureValueAction))  {
			return true;
		}
		return false;
	}
}
